﻿using Kingdee.BOS;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Newtonsoft.Json.Linq;
using SDWXServer.KD;
using SDWXServer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Description;

namespace SDWXServer.Controllers
{
    /// <summary>
    /// 码上吃饭
    /// </summary>
    public class MSCFController : ApiController
    {
        /// <summary>
        /// Test
        /// </summary>
        /// <param name="userToken">用户令牌</param>
        /// <returns></returns>
        [HttpGet]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult Test(string userToken)
        {
            var dataJson = new JArray();
            var result = new WebApiResult();
            result.Successful = true;
            result.Data["Data"] = dataJson;

            try
            {
                if (string.IsNullOrWhiteSpace(userToken))
                {
                    userToken = Guid.NewGuid().ToString();
                }
                var user = RedisManager.GetObject<WXUser>(userToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", userToken));
                }

                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }

            return Json<WebApiResult>(result);
        }







        /// <summary>
        /// 查询员工
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult QDEmpBaseBill(QueryBaseParam query)
        {
            var queryResult = new QueryEmpResult();
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                if (query == null) throw new Exception("请指定查询参数");
                if (string.IsNullOrWhiteSpace(query.UserToken)) throw new Exception("userToken不允许为空");

                var user = RedisManager.GetObject<WXUser>(query.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", query.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");

                var empName = query.Name;
                queryResult.UserToken = query.UserToken;
                queryResult.QuerySessionKey = query.QuerySessionKey;
                queryResult.PageIndex = query.PageIndex;

                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey))
                {
                    var empDatas = GetEmpinfoDatas(empName);
                    if (empDatas.Count > 0)
                    {
                        var lstEmpKey = new List<string>();
                        foreach (var obj in empDatas)
                        {
                            var fid = Convert.ToInt64(obj["FID"]);
                            var key = string.Format("SZGD_Empinfo_{0}", Convert.ToInt64(obj["FId"]));
                            var pkids = new object[] { fid };
                            var wrzs = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
                            var empBillObject = wrzs.LoadBillData("BD_Empinfo", pkids);

                            if (empBillObject.Length == 0) throw new Exception("加载员工为空");
                            var empobj = empBillObject[0] as DynamicObject;
                            var item = WXEmpinfo.Parse(empobj);

                            RedisManager.SetObject<WXEmpinfo>(item, new TimeSpan(0, 15, 0));
                            lstEmpKey.Add(key);
                        }
                        if (lstEmpKey.Count > 0)
                        {
                            queryResult.QuerySessionKey = Guid.NewGuid().ToString();
                            queryResult.TotalCount = lstEmpKey.Count;
                            queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstEmpKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);
                            queryResult.PageIndex = 0;

                            RedisManager.SetValue<List<string>>(queryResult.QuerySessionKey, lstEmpKey, new TimeSpan(0, 15, 0));
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey) == false)
                {
                    var lstEmpKey = RedisManager.GetValue<List<string>>(queryResult.QuerySessionKey);
                    if (lstEmpKey == null) throw new Exception("无效的QuerySessionKey");
                    queryResult.TotalCount = lstEmpKey.Count;
                    queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstEmpKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);

                    var beginIndex = queryResult.PageIndex * 20;
                    var endIndex = beginIndex + 20;
                    for (var index = beginIndex; index < endIndex; index++)
                    {
                        if (index >= lstEmpKey.Count) break;

                        var key = lstEmpKey[index];
                        var ygxx = RedisManager.GetObject<WXEmpinfo>(key);
                        if (ygxx == null) continue;

                        queryResult.Bills.Add(ygxx);
                    }
                }
                result.Data = JObject.FromObject(queryResult);
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            return Json<WebApiResult>(result);
        }

        /// <summary>
        /// 查询组织机构
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult QDOrgBaseBill(QueryBaseParam query)
        {
            var queryResult = new QueryOrgResult();
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                if (query == null) throw new Exception("请指定查询参数");
                if (string.IsNullOrWhiteSpace(query.UserToken)) throw new Exception("userToken不允许为空");

                var user = RedisManager.GetObject<WXUser>(query.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", query.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");
                var supplierName = query.Name;
                //List<Int64> orgIds = new List<Int64>();
                queryResult.UserToken = query.UserToken;
                queryResult.QuerySessionKey = query.QuerySessionKey;
                queryResult.PageIndex = query.PageIndex;

                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey))
                {
                    var orgIds = GetOrgIds(erpUserId);
                    if (orgIds.Count > 0)
                    {
                        var lstOrgKey = new List<string>();
                        foreach (var orgId in orgIds)
                        {
                            var fid = orgId;
                            var key = string.Format("MSXZ_Org_{0}", orgId);
                            var pkids = new object[] { fid };
                            var wrzs = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
                            var orgBillObject = wrzs.LoadBillData("ORG_Organizations", pkids);

                            if (orgBillObject.Length == 0) throw new Exception("加载组织机构为空");
                            var orgobj = orgBillObject[0] as DynamicObject;
                            var item = WXMSXZOrg.Parse(orgobj);

                            RedisManager.SetObject<WXMSXZOrg>(item, new TimeSpan(0, 15, 0));
                            lstOrgKey.Add(key);
                        }
                        if (lstOrgKey.Count > 0)
                        {
                            queryResult.QuerySessionKey = Guid.NewGuid().ToString();
                            queryResult.TotalCount = lstOrgKey.Count;
                            queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstOrgKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);
                            queryResult.PageIndex = 0;

                            RedisManager.SetValue<List<string>>(queryResult.QuerySessionKey, lstOrgKey, new TimeSpan(0, 15, 0));
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey) == false)
                {
                    var lstOrgKey = RedisManager.GetValue<List<string>>(queryResult.QuerySessionKey);
                    if (lstOrgKey == null) throw new Exception("无效的QuerySessionKey");
                    queryResult.TotalCount = lstOrgKey.Count;
                    queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstOrgKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);

                    var beginIndex = queryResult.PageIndex * 20;
                    var endIndex = beginIndex + 20;
                    for (var index = beginIndex; index < endIndex; index++)
                    {
                        if (index >= lstOrgKey.Count) break;

                        var key = lstOrgKey[index];
                        var org = RedisManager.GetObject<WXMSXZOrg>(key);
                        if (org == null) continue;

                        queryResult.Bills.Add(org);
                    }
                }
                result.Data = JObject.FromObject(queryResult);
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            return Json<WebApiResult>(result);
        }



        /// <summary>
        /// 查询餐品
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult QDCanPinBill(QueryBaseParam query)
        {
            var queryResult = new QueryCpResult();
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                if (query == null) throw new Exception("请指定查询参数");
                if (string.IsNullOrWhiteSpace(query.UserToken)) throw new Exception("userToken不允许为空");

                var user = RedisManager.GetObject<WXUser>(query.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", query.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");

                var CanPinName = query.Name;
                queryResult.UserToken = query.UserToken;
                queryResult.QuerySessionKey = query.QuerySessionKey;
                queryResult.PageIndex = query.PageIndex;

                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey))
                {
                    var empDatas = GetCanPinDatas(CanPinName);
                    if (empDatas.Count > 0)
                    {
                        var lstEmpKey = new List<string>();
                        foreach (var obj in empDatas)
                        {
                            var fid = Convert.ToInt64(obj["FID"]);
                            var key = string.Format("MSCF_CanPin_{0}", Convert.ToInt64(obj["FId"]));
                            var pkids = new object[] { fid };
                            var wrzs = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
                            var empBillObject = wrzs.LoadBillData("BSD_CP", pkids);

                            if (empBillObject.Length == 0) throw new Exception("加载餐品为空");
                            var empobj = empBillObject[0] as DynamicObject;
                            var item = WXCanPin.Parse(empobj);

                            RedisManager.SetObject<WXCanPin>(item, new TimeSpan(0, 15, 0));
                            lstEmpKey.Add(key);
                        }
                        if (lstEmpKey.Count > 0)
                        {
                            queryResult.QuerySessionKey = Guid.NewGuid().ToString();
                            queryResult.TotalCount = lstEmpKey.Count;
                            queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstEmpKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);
                            queryResult.PageIndex = 0;

                            RedisManager.SetValue<List<string>>(queryResult.QuerySessionKey, lstEmpKey, new TimeSpan(0, 15, 0));
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey) == false)
                {
                    var lstEmpKey = RedisManager.GetValue<List<string>>(queryResult.QuerySessionKey);
                    if (lstEmpKey == null) throw new Exception("无效的QuerySessionKey");
                    queryResult.TotalCount = lstEmpKey.Count;
                    queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstEmpKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);

                    var beginIndex = queryResult.PageIndex * 20;
                    var endIndex = beginIndex + 20;
                    for (var index = beginIndex; index < endIndex; index++)
                    {
                        if (index >= lstEmpKey.Count) break;

                        var key = lstEmpKey[index];
                        var cpxx = RedisManager.GetObject<WXCanPin>(key);
                        if (cpxx == null) continue;

                        queryResult.Bills.Add(cpxx);
                    }
                }
                result.Data = JObject.FromObject(queryResult);
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            return Json<WebApiResult>(result);
        }




        /// <summary>
        /// 查询业务分类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult QDYeWuFenLeiBill(QueryBaseParam query)
        {
            var queryResult = new QueryYwflResult();
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                if (query == null) throw new Exception("请指定查询参数");
                if (string.IsNullOrWhiteSpace(query.UserToken)) throw new Exception("userToken不允许为空");

                var user = RedisManager.GetObject<WXUser>(query.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", query.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");

                //var CanPinName = query.Name;
                queryResult.UserToken = query.UserToken;
                queryResult.QuerySessionKey = query.QuerySessionKey;
                //queryResult.PageIndex = query.PageIndex;

                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey))
                {
                    var rylxDatas = GetYeWuFenLeiDatas();
                    if (rylxDatas.Count > 0)
                    {
                        var lstRylxKey = new List<string>();

                        foreach (var obj in rylxDatas)
                        {
                            var item = WXRYeWuFenLei.Parse(obj);

                            item.YeWuFenLeiValue = Convert.ToString(obj["FVALUE"]);
                            item.YeWuFenLeiName = Convert.ToString(obj["FCAPTION"]);

                            RedisManager.SetObject<WXRYeWuFenLei>(item, new TimeSpan(0, 15, 0));
                            queryResult.Bills.Add(item);
                        }
                    }
                }
                result.Data = JObject.FromObject(queryResult);
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            return Json<WebApiResult>(result);
        }


        /// <summary>
        /// 查询车辆人员
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult QDCLRYBill(QueryBaseParam query)
        {
            var queryResult = new QueryCLRYResult();
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                if (query == null) throw new Exception("请指定查询参数");
                if (string.IsNullOrWhiteSpace(query.UserToken)) throw new Exception("userToken不允许为空");

                var user = RedisManager.GetObject<WXUser>(query.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", query.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");

                var CanPinName = query.Name;
                queryResult.UserToken = query.UserToken;
                queryResult.QuerySessionKey = query.QuerySessionKey;
                queryResult.PageIndex = query.PageIndex;

                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey))
                {
                    var empDatas = GetCLRYDatas();
                    if (empDatas.Count > 0)
                    {
                        var lstEmpKey = new List<string>();
                        foreach (var obj in empDatas)
                        {
                            var fid = Convert.ToInt64(obj["FID"]);
                            var key = string.Format("MSCF_CheLiangRenYuan_{0}", Convert.ToInt64(obj["FId"]));
                            var pkids = new object[] { fid };
                            var wrzs = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
                            var empBillObject = wrzs.LoadBillData("SD_ZN_CLRY", pkids);

                            if (empBillObject.Length == 0) throw new Exception("加载车辆人员为空");
                            var empobj = empBillObject[0] as DynamicObject;
                            var item = WXCLRY.Parse(empobj);

                            RedisManager.SetObject<WXCLRY>(item, new TimeSpan(0, 15, 0));
                            lstEmpKey.Add(key);
                        }
                        if (lstEmpKey.Count > 0)
                        {
                            queryResult.QuerySessionKey = Guid.NewGuid().ToString();
                            queryResult.TotalCount = lstEmpKey.Count;
                            queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstEmpKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);
                            queryResult.PageIndex = 0;

                            RedisManager.SetValue<List<string>>(queryResult.QuerySessionKey, lstEmpKey, new TimeSpan(0, 15, 0));
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey) == false)
                {
                    var lstEmpKey = RedisManager.GetValue<List<string>>(queryResult.QuerySessionKey);
                    if (lstEmpKey == null) throw new Exception("无效的QuerySessionKey");
                    queryResult.TotalCount = lstEmpKey.Count;
                    queryResult.PageCount = (Int32)Kingdee.BOS.MathUtil.Round(lstEmpKey.Count / 20m, 0, Kingdee.BOS.RoundMode.Carry);

                    var beginIndex = queryResult.PageIndex * 20;
                    var endIndex = beginIndex + 20;
                    for (var index = beginIndex; index < endIndex; index++)
                    {
                        if (index >= lstEmpKey.Count) break;

                        var key = lstEmpKey[index];
                        var cpxx = RedisManager.GetObject<WXCLRY>(key);
                        if (cpxx == null) continue;

                        queryResult.Bills.Add(cpxx);
                    }
                }
                result.Data = JObject.FromObject(queryResult);
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            return Json<WebApiResult>(result);
        }



        /// <summary>
        /// 业务分类查询结果
        /// </summary>
        public class QueryYwflResult
        {
            public QueryYwflResult()
            {
                this.Bills = new List<WXRYeWuFenLei>();
            }
            /// <summary> 
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 数量
            /// </summary>
            public Int32 TotalCount { get; set; }
            /// <summary>
            /// 人员类型数据
            /// </summary>
            public List<WXRYeWuFenLei> Bills { get; set; }
        }

        /// <summary>
        /// 车辆人员查询结果
        /// </summary>
        public class QueryCLRYResult
        {
            public QueryCLRYResult()
            {
                this.Bills = new List<WXCLRY>();
            }
            /// <summary> 
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 员工数量
            /// </summary>
            public Int32 TotalCount { get; set; }
            /// <summary>
            /// 总页数
            /// </summary>
            public Int32 PageCount { get; set; }
            /// <summary>
            /// 当前第几页
            /// </summary>
            public Int32 PageIndex { get; set; }
            /// <summary>
            /// 员工数据
            /// </summary>
            public List<WXCLRY> Bills { get; set; }
        }





        /// <summary>
        /// 查询证件类型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult QDZhengJianLeiXingBill(QueryBaseParam query)
        {
            var queryResult = new QueryZjlxResult();
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                if (query == null) throw new Exception("请指定查询参数");
                if (string.IsNullOrWhiteSpace(query.UserToken)) throw new Exception("userToken不允许为空");

                var user = RedisManager.GetObject<WXUser>(query.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", query.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");

                //var CanPinName = query.Name;
                queryResult.UserToken = query.UserToken;
                queryResult.QuerySessionKey = query.QuerySessionKey;
                //queryResult.PageIndex = query.PageIndex;

                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey))
                {
                    var zjlxDatas = GetZhengJianLeiXingDatas();
                    if (zjlxDatas.Count > 0)
                    {
                        var lstZjlxKey = new List<string>();

                        foreach (var obj in zjlxDatas)
                        {
                            var item = WXZhengJianLeiXing.Parse(obj);

                            item.ZhengJianLeiXingValue = Convert.ToString(obj["FVALUE"]);
                            item.ZhengJianLeiXingName = Convert.ToString(obj["FCAPTION"]);

                            RedisManager.SetObject<WXZhengJianLeiXing>(item, new TimeSpan(0, 15, 0));
                            queryResult.Bills.Add(item);
                        }
                    }
                }
                result.Data = JObject.FromObject(queryResult);
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            return Json<WebApiResult>(result);
        }


        /// <summary>
        /// 查询人员类型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult QDRenYuanLeiXingBill(QueryBaseParam query)
        {
            var queryResult = new QueryRylxResult();
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                if (query == null) throw new Exception("请指定查询参数");
                if (string.IsNullOrWhiteSpace(query.UserToken)) throw new Exception("userToken不允许为空");

                var user = RedisManager.GetObject<WXUser>(query.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", query.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion

                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");

                //var CanPinName = query.Name;
                queryResult.UserToken = query.UserToken;
                queryResult.QuerySessionKey = query.QuerySessionKey;
                //queryResult.PageIndex = query.PageIndex;

                if (string.IsNullOrWhiteSpace(queryResult.QuerySessionKey))
                {
                    var rylxDatas = GetRenYuanLeiXingDatas();
                    if (rylxDatas.Count > 0)
                    {
                        var lstRylxKey = new List<string>();

                        foreach (var obj in rylxDatas)
                        {
                            var item = WXRenYuanLeiXing.Parse(obj);

                            item.RenYuanLeiXingValue = Convert.ToString(obj["FVALUE"]);
                            item.RenYuanLeiXingName = Convert.ToString(obj["FCAPTION"]);

                            RedisManager.SetObject<WXRenYuanLeiXing>(item, new TimeSpan(0, 15, 0));
                            queryResult.Bills.Add(item);
                        }
                    }
                }
                result.Data = JObject.FromObject(queryResult);
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            return Json<WebApiResult>(result);
        }





        /// <summary>
        /// 食堂管理新增保存提交单据
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult NewSaveSubmitBill([FromBody] NewSaveSubmitBillParam datas)
        {
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                var user = RedisManager.GetObject<WXUser>(datas.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", datas.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion
                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");
                //if (datas.BillId == 0L)
                //{
                //    throw new Exception("禁止操作：单据内码不能为空");
                //}
                if (string.IsNullOrWhiteSpace(datas.BillFormId))
                {
                    throw new Exception("禁止操作：单据FormId不能为空");
                }

                if (datas.BillFormId == "BSD_XFJLD")
                {
                    var formId = datas.BillFormId;
                    var billdata = new JSONObject();
                    billdata["BillId"] = 0L;
                    billdata["UserId"] = erpUserId;
                    billdata["F_BSD_OrgId"] = datas.OrgId;
                    billdata["DocumentStatus"] = datas.DocumentStatus;
                    billdata["F_BSD_STAFF"] = datas.Data.Value<string>("F_BSD_STAFF");

                    billdata["F_BSD_PHONE"] = datas.Data.Value<string>("F_BSD_PHONE");

                    billdata["F_BSD_CPNUMBER"] = datas.Data.Value<string>("F_BSD_CPNUMBER");
                    //billdata["F_BSD_CPNAME"] = cpobj.Value<string>("FName");
                    //billdata["F_BSD_CPJE"] = cpobj.Value<decimal>("F_BSD_CPJE");


                    billdata["F_BSD_CPSL"] = datas.Data.Value<Int64>("F_BSD_CPSL");
                    billdata["F_BSD_BZ"] = datas.Data.Value<string>("F_BSD_BZ");



                    var client = new SD.MSXZ.K3.ServiceFacade.KDServiceClient.MSXZClientProxy();
                    var saveResult = client.SZGDNewSaveSubmitBill(formId, billdata);

                    KD.Logger.Info(string.Format("NewSaveSubmitBill-{0}:{1}", formId, billdata.ToString()));
                    Logger.Info(saveResult.Message);
                    result.Successful = saveResult.Successful;
                    result.Message = saveResult.Message;
                }

                if (datas.BillFormId == "BSD_CZJLD")
                {
                    var formId = datas.BillFormId;
                    var billdata = new JSONObject();
                    billdata["BillId"] = 0L;
                    billdata["UserId"] = erpUserId;
                    billdata["F_BSD_OrgId"] = datas.OrgId;
                    billdata["DocumentStatus"] = datas.DocumentStatus;
                    billdata["F_BSD_STAFF"] = datas.Data.Value<string>("F_BSD_STAFF");
                    billdata["F_BSD_PHONE"] = datas.Data.Value<string>("F_BSD_PHONE");
                    billdata["F_BSD_JINE"] = datas.Data.Value<decimal>("F_BSD_JINE");
                    billdata["F_BSD_CZDate"] = datas.Data.Value<DateTime>("F_BSD_CZDate");

                    var client = new SD.MSXZ.K3.ServiceFacade.KDServiceClient.MSXZClientProxy();
                    var saveResult = client.SZGDNewSaveSubmitBill(formId, billdata);
                    KD.Logger.Info(string.Format("NewSaveSubmitBill-{0}:{1}", formId, billdata.ToString()));
                    Logger.Info(saveResult.Message);
                    result.Successful = saveResult.Successful;
                    result.Message = saveResult.Message;
                }

                if (datas.BillFormId == "BSD_WLRYYCJLD")
                {
                    var formId = datas.BillFormId;
                    var billdata = new JSONObject();
                    billdata["BillId"] = 0L;
                    billdata["UserId"] = erpUserId;
                    billdata["F_BSD_OrgId"] = datas.OrgId;
                    billdata["DocumentStatus"] = datas.DocumentStatus;

                    billdata["F_SD_XM"] = datas.Data.Value<string>("F_SD_XM");
                    billdata["F_SD_SJH"] = datas.Data.Value<string>("F_SD_SJH");

                    billdata["F_SD_SXRQ"] = datas.Data.Value<DateTime>("F_SD_SXRQ");
                    billdata["F_SD_SHXRQ"] = datas.Data.Value<DateTime>("F_SD_SHXRQ");

                    var client = new SD.MSXZ.K3.ServiceFacade.KDServiceClient.MSXZClientProxy();
                    var saveResult = client.SZGDNewSaveSubmitBill(formId, billdata);
                    KD.Logger.Info(string.Format("NewSaveSubmitBill-{0}:{1}", formId, billdata.ToString()));
                    Logger.Info(saveResult.Message);
                    result.Successful = saveResult.Successful;
                    result.Message = saveResult.Message;
                }

            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;

                Logger.Error("NewSaveSubmitBill", e);
            }
            return Json<WebApiResult>(result);
        }


        /// <summary>
        /// 码上拜访新增保存提交单据
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        [HttpPost]
        [ResponseType(typeof(WebApiResult))]
        public IHttpActionResult BFNewSaveSubmitBill([FromBody] NewSaveSubmitBillParam datas)
        {
            var result = new WebApiResult();
            result.Successful = true;
            try
            {
                var user = RedisManager.GetObject<WXUser>(datas.UserToken);
                if (user == null)
                {
                    throw new Exception(string.Format("无效的userToken:{0}", datas.UserToken));
                }
                #region 金蝶云登录验证
                LoginViewModel.CheckLogin();
                if (LoginViewModel.Current.LoginSuccessful == false)
                {
                    throw new Exception("金蝶云登录失败");
                }
                #endregion
                var erpUserId = user.ERPUserId;
                //if (erpUserId == 0L) throw new Exception("请首先注册为ERP用户");
                //if (datas.BillId == 0L)
                //{
                //    throw new Exception("禁止操作：单据内码不能为空");
                //}
                if (string.IsNullOrWhiteSpace(datas.BillFormId))
                {
                    throw new Exception("禁止操作：单据FormId不能为空");
                }

                if (datas.BillFormId == "BSD_FKDJD")
                {
                    var fkdjdEntrys = new JArray();
                    //if (datas.BillId > 0L)
                    //{
                    fkdjdEntrys = datas.Data.Value<JArray>("EntryRows");
                    //}
                    var fkdjdEntrys_Save = new JSONArray();
                    foreach (JObject fkdjdEntry in fkdjdEntrys)
                    {
                        var item = new JSONObject();
                        item["EntryId"] = fkdjdEntry.Value<Int64>("EntryId");
                        //人员姓名
                        item["F_SD_RYXM"] = fkdjdEntry.Value<string>("F_SD_RYXM");
                        //证件类型
                        item["F_SD_ZJLX"] = fkdjdEntry.Value<Int64>("F_SD_ZJLX");
                        //证件号码
                        item["F_SD_ZJHM"] = fkdjdEntry.Value<string>("F_SD_ZJHM");
                        //外来人员联系电话
                        item["F_SD_LXDHH"] = fkdjdEntry.Value<string>("F_SD_LXDHH");
                        //人数
                        item["F_SD_RS"] = fkdjdEntry.Value<Int64>("F_SD_RS");
                        //车牌号
                        item["F_SD_CPH"] = fkdjdEntry.Value<string>("F_SD_CPH");
                        //拜访事由   
                        item["F_SD_BFSY"] = fkdjdEntry.Value<string>("F_SD_BFSY");
                        //车辆入场
                        item["F_SD_CLRC"] = fkdjdEntry.Value<string>("F_SD_CLRC");
                        //车辆称重
                        item["F_SD_CZ"] = fkdjdEntry.Value<string>("F_SD_CZ");
                        //备注
                        item["F_SD_BZ"] = fkdjdEntry.Value<string>("F_SD_BZ");


                        fkdjdEntrys_Save.Add(item);
                    }



                    var formId = datas.BillFormId;
                    var billdata = new JSONObject();
                    billdata["BillId"] = 0L;
                    //billdata["UserId"] = erpUserId;
                    //组织
                    billdata["F_SD_ZZ"] = datas.OrgId;
                    //单据状态
                    billdata["DocumentStatus"] = datas.DocumentStatus;
                    //申请人
                    billdata["F_SD_SQR"] = datas.Data.Value<string>("F_SD_SQR");
                    //员工
                    billdata["F_SD_YG"] = datas.Data.Value<string>("F_SD_YG");
                    //员工联系电话
                    billdata["F_SD_LXDH"] = datas.Data.Value<string>("F_SD_LXDH");
                    //拜访事项
                    billdata["F_SD_BFSQ"] = datas.Data.Value<string>("F_SD_BFSQ");
                    //起始日期
                    billdata["F_SD_QSRQ"] = datas.Data.Value<DateTime>("F_SD_QSRQ");
                    //结束日期
                    billdata["F_SD_JSRQ"] = datas.Data.Value<DateTime>("F_SD_JSRQ");
                    //业务分类
                    billdata["F_SD_YWFL"] = datas.Data.Value<Int64>("F_SD_YWFL");
                    //车辆
                    billdata["F_SD_CL"] = datas.Data.Value<string>("F_SD_CL");
                    //拜访地点
                    billdata["F_BFDD"] = datas.Data.Value<string>("F_BFDD");

                    //单据体
                    billdata["EntryDatas"] = fkdjdEntrys_Save;




                    var client = new SD.MSXZ.K3.ServiceFacade.KDServiceClient.MSXZClientProxy();
                    var saveResult = client.SZGDNewSaveSubmitBill(formId, billdata);

                    KD.Logger.Info(string.Format("NewSaveSubmitBill-{0}:{1}", formId, billdata.ToString()));
                    Logger.Info(saveResult.Message);
                    result.Successful = saveResult.Successful;
                    result.Message = saveResult.Message;
                }


                if (datas.BillFormId == "BSD_RCJLD")
                {
                    var rcjldEntrys = new JArray();
                    //if (datas.BillId > 0L)
                    //{
                    rcjldEntrys = datas.Data.Value<JArray>("EntryRows");
                    //}
                    var rcjldEntrys_Save = new JSONArray();
                    foreach (JObject rcjldEntry in rcjldEntrys)
                    {
                        var item = new JSONObject();
                        item["EntryId"] = rcjldEntry.Value<Int64>("EntryId");
                        //人员姓名
                        item["F_SD_RYXM"] = rcjldEntry.Value<string>("F_SD_RYXM");
                        //证件类型
                        item["F_SD_ZJLX"] = rcjldEntry.Value<Int64>("F_SD_ZJLX");
                        //证件号码
                        item["F_SD_ZJHM"] = rcjldEntry.Value<string>("F_SD_ZJHM");
                        //外来人员联系电话
                        item["F_SD_LXDHH"] = rcjldEntry.Value<string>("F_SD_LXDHH");
                        //人数
                        item["F_SD_RS"] = rcjldEntry.Value<Int64>("F_SD_RS");
                        //车牌号
                        item["F_SD_CPH"] = rcjldEntry.Value<string>("F_SD_CPH");
                        //拜访事由   
                        item["F_SD_BFSY"] = rcjldEntry.Value<string>("F_SD_BFSY");
                        //车辆入场
                        item["F_SD_CLRC"] = rcjldEntry.Value<string>("F_SD_CLRC");
                        //车辆称重
                        item["F_SD_CZ"] = rcjldEntry.Value<string>("F_SD_CZ");
                        //人员类型
                        item["F_SD_RYLX"] = rcjldEntry.Value<Int64>("F_SD_RYLX");
                        //备注
                        item["F_SD_BZ"] = rcjldEntry.Value<string>("F_SD_BZ");



                        rcjldEntrys_Save.Add(item);
                    }



                    var formId = datas.BillFormId;
                    var billdata = new JSONObject();
                    billdata["BillId"] = 0L;
                    //billdata["UserId"] = erpUserId;
                    //组织
                    billdata["F_SD_ZZ"] = datas.OrgId;
                    //单据状态
                    billdata["DocumentStatus"] = datas.DocumentStatus;
                    //申请人
                    billdata["F_SD_SQR"] = datas.Data.Value<string>("F_SD_SQR");
                    //员工
                    billdata["F_SD_YG"] = datas.Data.Value<string>("F_SD_YG");
                    //员工联系电话
                    billdata["F_SD_LXDH"] = datas.Data.Value<string>("F_SD_LXDH");
                    //拜访事项
                    billdata["F_SD_BFSQ"] = datas.Data.Value<string>("F_SD_BFSQ");
                    //起始日期
                    billdata["F_SD_QSRQ"] = datas.Data.Value<DateTime>("F_SD_QSRQ");
                    //结束日期
                    billdata["F_SD_JSRQ"] = datas.Data.Value<DateTime>("F_SD_JSRQ");

                    //单据体
                    billdata["EntryDatas"] = rcjldEntrys_Save;



                    var client = new SD.MSXZ.K3.ServiceFacade.KDServiceClient.MSXZClientProxy();
                    var saveResult = client.SZGDNewSaveSubmitBill(formId, billdata);

                    KD.Logger.Info(string.Format("NewSaveSubmitBill-{0}:{1}", formId, billdata.ToString()));
                    Logger.Info(saveResult.Message);
                    result.Successful = saveResult.Successful;
                    result.Message = saveResult.Message;
                }


                if (datas.BillFormId == "BSD_CCJLD")
                {
                    var ccjldEntrys = new JArray();
                    //if (datas.BillId > 0L)
                    //{
                    ccjldEntrys = datas.Data.Value<JArray>("EntryRows");
                    //}
                    var ccjldEntrys_Save = new JSONArray();
                    foreach (JObject ccjldEntry in ccjldEntrys)
                    {
                        var item = new JSONObject();
                        item["EntryId"] = ccjldEntry.Value<Int64>("EntryId");
                        //人员姓名
                        item["F_SD_RYXM"] = ccjldEntry.Value<string>("F_SD_RYXM");
                        //证件类型
                        item["F_SD_ZJLX"] = ccjldEntry.Value<Int64>("F_SD_ZJLX");
                        //证件号码
                        item["F_SD_ZJHM"] = ccjldEntry.Value<string>("F_SD_ZJHM");
                        //外来人员联系电话
                        item["F_SD_LXDHH"] = ccjldEntry.Value<string>("F_SD_LXDHH");
                        //人数
                        item["F_SD_RS"] = ccjldEntry.Value<Int64>("F_SD_RS");
                        //车牌号
                        item["F_SD_CPH"] = ccjldEntry.Value<string>("F_SD_CPH");
                        //拜访事由   
                        item["F_SD_BFSY"] = ccjldEntry.Value<string>("F_SD_BFSY");
                        //车辆入场
                        item["F_SD_CLRC"] = ccjldEntry.Value<string>("F_SD_CLRC");
                        //车辆称重
                        item["F_SD_CZ"] = ccjldEntry.Value<string>("F_SD_CZ");
                        //人员类型
                        item["F_SD_RYLX"] = ccjldEntry.Value<Int64>("F_SD_RYLX");
                        //备注
                        item["F_SD_BZ"] = ccjldEntry.Value<string>("F_SD_BZ");



                        ccjldEntrys_Save.Add(item);
                    }



                    var formId = datas.BillFormId;
                    var billdata = new JSONObject();
                    billdata["BillId"] = 0L;
                    //billdata["UserId"] = erpUserId;
                    //组织
                    billdata["F_SD_ZZ"] = datas.OrgId;
                    //单据状态
                    billdata["DocumentStatus"] = datas.DocumentStatus;
                    //申请人
                    billdata["F_SD_SQR"] = datas.Data.Value<string>("F_SD_SQR");
                    //员工
                    billdata["F_SD_YG"] = datas.Data.Value<string>("F_SD_YG");
                    //员工联系电话
                    billdata["F_SD_LXDH"] = datas.Data.Value<string>("F_SD_LXDH");
                    //拜访事项
                    billdata["F_SD_BFSQ"] = datas.Data.Value<string>("F_SD_BFSQ");
                    //起始日期
                    billdata["F_SD_QSRQ"] = datas.Data.Value<DateTime>("F_SD_QSRQ");
                    //结束日期
                    billdata["F_SD_JSRQ"] = datas.Data.Value<DateTime>("F_SD_JSRQ");

                    //单据体
                    billdata["EntryDatas"] = ccjldEntrys_Save;




                    var client = new SD.MSXZ.K3.ServiceFacade.KDServiceClient.MSXZClientProxy();
                    var saveResult = client.SZGDNewSaveSubmitBill(formId, billdata);

                    KD.Logger.Info(string.Format("NewSaveSubmitBill-{0}:{1}", formId, billdata.ToString()));
                    Logger.Info(saveResult.Message);
                    result.Successful = saveResult.Successful;
                    result.Message = saveResult.Message;
                }

            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;

                Logger.Error("NewSaveSubmitBill", e);
            }
            return Json<WebApiResult>(result);
        }





        /// <summary>
        /// 基础资料查询参数
        /// </summary>
        public class QueryBaseParam
        {
            /// <summary>
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 内码
            /// </summary>
            public Int64 BillId { get; set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 组织机构
            /// </summary>
            public Int64 OrgId { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 分页：第几页，索引值从0开始
            /// </summary>
            public Int32 PageIndex { get; set; }
        }

        /// <summary>
        /// 员工查询结果
        /// </summary>
        public class QueryEmpResult
        {
            public QueryEmpResult()
            {
                this.Bills = new List<WXEmpinfo>();
            }
            /// <summary> 
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 员工数量
            /// </summary>
            public Int32 TotalCount { get; set; }
            /// <summary>
            /// 总页数
            /// </summary>
            public Int32 PageCount { get; set; }
            /// <summary>
            /// 当前第几页
            /// </summary>
            public Int32 PageIndex { get; set; }
            /// <summary>
            /// 员工数据
            /// </summary>
            public List<WXEmpinfo> Bills { get; set; }
        }

        /// <summary>
        /// 组织机构查询结果
        /// </summary>
        public class QueryOrgResult
        {
            public QueryOrgResult()
            {
                this.Bills = new List<WXMSXZOrg>();
            }
            /// <summary>
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 组织机构数量
            /// </summary>
            public Int32 TotalCount { get; set; }
            /// <summary>
            /// 总页数
            /// </summary>
            public Int32 PageCount { get; set; }
            /// <summary>
            /// 当前第几页
            /// </summary>
            public Int32 PageIndex { get; set; }
            /// <summary>
            /// 组织机构数据
            /// </summary>
            public List<WXMSXZOrg> Bills { get; set; }
        }


        /// <summary>
        /// 餐品查询结果
        /// </summary>
        public class QueryCpResult
        {
            public QueryCpResult()
            {
                this.Bills = new List<WXCanPin>();
            }
            /// <summary> 
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 餐品数量
            /// </summary>
            public Int32 TotalCount { get; set; }
            /// <summary>
            /// 总页数
            /// </summary>
            public Int32 PageCount { get; set; }
            /// <summary>
            /// 当前第几页
            /// </summary>
            public Int32 PageIndex { get; set; }
            /// <summary>
            /// 餐品数据
            /// </summary>
            public List<WXCanPin> Bills { get; set; }
        }


        /// <summary>
        /// 证件类型查询结果
        /// </summary>
        public class QueryZjlxResult
        {
            public QueryZjlxResult()
            {
                this.Bills = new List<WXZhengJianLeiXing>();
            }
            /// <summary> 
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 数量
            /// </summary>
            public Int32 TotalCount { get; set; }
            /// <summary>
            /// 证件类型数据
            /// </summary>
            public List<WXZhengJianLeiXing> Bills { get; set; }
        }

        /// <summary>
        /// 人员类型查询结果
        /// </summary>
        public class QueryRylxResult
        {
            public QueryRylxResult()
            {
                this.Bills = new List<WXRenYuanLeiXing>();
            }
            /// <summary> 
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            /// 查询Session
            /// </summary>
            public string QuerySessionKey { get; set; }
            /// <summary>
            /// 数量
            /// </summary>
            public Int32 TotalCount { get; set; }
            /// <summary>
            /// 人员类型数据
            /// </summary>
            public List<WXRenYuanLeiXing> Bills { get; set; }
        }


        /// <summary>
        /// 新增保存提交单据参数
        /// </summary>
        public class NewSaveSubmitBillParam
        {
            public NewSaveSubmitBillParam()
            {
                this.Data = new JObject();
            }
            /// <summary>
            /// 用户令牌
            /// </summary>
            public string UserToken { get; set; }
            /// <summary>
            ///  单据FormId
            /// </summary>
            public string BillFormId { get; set; }
            /// <summary>
            /// 单据内码
            /// </summary>
            public Int64 BillId { get; set; }
            /// <summary>
            /// 单据状态
            /// </summary>
            public string DocumentStatus { get; set; }
            /// <summary>
            /// 组织内码
            /// </summary>
            public Int64 OrgId { get; set; }
            /// <summary>
            /// 数据
            /// </summary>
            public JObject Data { get; set; }
        }



        DynamicObjectCollection GetEmpinfoDatas(string empName)
        {
            DynamicObjectCollection objs = null;
            var filterName = "";
            if (string.IsNullOrWhiteSpace(empName) == false)
            {
                filterName = string.Format("AND T2.FNAME LIKE '%{0}%'", empName);
            }
            var sql = string.Format(@"
select  T1.FID,T1.F_BSD_ZHJE,T1.FMOBILE
FROM T_HR_EMPINFO T1
INNER JOIN T_HR_EMPINFO_L T2 ON T2.FID=T1.FID AND T2.FLocaleID=2052
where T1.FDOCUMENTSTATUS='C'
{0}
;

", filterName);
            var lst = new List<Kingdee.BOS.SqlParam>();
            var sqlSvr = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            objs = sqlSvr.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);

            return objs;
        }

        DynamicObjectCollection GetCanPinDatas(string CanPin)
        {
            DynamicObjectCollection objs = null;

            //var filterName = "";
            //if (string.IsNullOrWhiteSpace(zljcxmName) == false)
            //{
            //    filterName = string.Format("AND T2.FNAME LIKE '%{0}%'", zljcxmName);
            //}
            var sql = string.Format(@"
        SELECT T1.FID,T1.F_BSD_CPJE,T2.FNAME 
FROM BSD_t_CP T1
INNER JOIN BSD_t_CP_L T2 ON T2.FID = T1.FID
WHERE T1.FDOCUMENTSTATUS = 'C'
        ");
            var lst = new List<Kingdee.BOS.SqlParam>();
            var sqlSvr = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            objs = sqlSvr.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);

            return objs;
        }



        DynamicObjectCollection GetZhengJianLeiXingDatas()
        {
            DynamicObjectCollection objs = null;


            var sql = string.Format(@"
        SELECT
T3.FVALUE,T4.FCAPTION
FROM
T_meta_formenum T1
INNER JOIN T_META_FORMENUM_L T2 ON T2.FID = T1.FID
INNER JOIN T_META_FORMENUMITEM T3 ON T3.FID = T1.FID
INNER JOIN T_META_FORMENUMITEM_L T4 ON T4.FENUMID = T3.FENUMID
WHERE T2.FNAME = '证件类型'
        ");
            var lst = new List<Kingdee.BOS.SqlParam>();
            var sqlSvr = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            objs = sqlSvr.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);

            return objs;
        }


        DynamicObjectCollection GetRenYuanLeiXingDatas()
        {
            DynamicObjectCollection objs = null;


            var sql = string.Format(@"
        SELECT
T3.FVALUE,T4.FCAPTION
FROM
T_meta_formenum T1
INNER JOIN T_META_FORMENUM_L T2 ON T2.FID = T1.FID
INNER JOIN T_META_FORMENUMITEM T3 ON T3.FID = T1.FID
INNER JOIN T_META_FORMENUMITEM_L T4 ON T4.FENUMID = T3.FENUMID
WHERE T2.FNAME = '人员类型'
        ");
            var lst = new List<Kingdee.BOS.SqlParam>();
            var sqlSvr = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            objs = sqlSvr.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);

            return objs;
        }


        DynamicObjectCollection GetYeWuFenLeiDatas()
        {
            DynamicObjectCollection objs = null;


            var sql = string.Format(@"
        SELECT
T3.FVALUE,T4.FCAPTION
FROM
T_meta_formenum T1
INNER JOIN T_META_FORMENUM_L T2 ON T2.FID = T1.FID
INNER JOIN T_META_FORMENUMITEM T3 ON T3.FID = T1.FID
INNER JOIN T_META_FORMENUMITEM_L T4 ON T4.FENUMID = T3.FENUMID
WHERE T2.FNAME = '业务分类'
        ");
            var lst = new List<Kingdee.BOS.SqlParam>();
            var sqlSvr = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            objs = sqlSvr.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);

            return objs;
        }



        DynamicObjectCollection GetCLRYDatas()
        {
            DynamicObjectCollection objs = null;

            //var filterName = "";
            //if (string.IsNullOrWhiteSpace(zljcxmName) == false)
            //{
            //    filterName = string.Format("AND T2.FNAME LIKE '%{0}%'", zljcxmName);
            //}
            var sql = string.Format(@"
SELECT
T1.FID,T2.FNAME,T1.FCARNUM
FROM
SD_ZN_CLRY T1
INNER JOIN SD_ZN_CLRY_L T2 ON T2.FID = T1.FID
WHERE T1.FDOCUMENTSTATUS = 'C'
        ");
            var lst = new List<Kingdee.BOS.SqlParam>();
            var sqlSvr = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            objs = sqlSvr.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);

            return objs;
        }

        /// <summary>
        /// 通过用户Id获取该用户员工信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        DynamicObjectCollection GetEmpInfos(Int64 userId)
        {

            DynamicObjectCollection objs = null;
            var sql = string.Format(@"
        SELECT T3.FID,T3.FNUMBER,T4.FNAME
        FROM T_HR_EMPINFO T3
        INNER JOIN T_HR_EMPINFO_L T4  ON T4.FID=T3.FID AND T4.FLOCALEID=2052
        INNER JOIN T_SEC_USER T5  ON T5.FLINKOBJECT=T3.FPERSONID
        WHERE T5.FUSERID=@userId
        ");

            var lst = new List<Kingdee.BOS.SqlParam>();
            lst.Add(new Kingdee.BOS.SqlParam("@userId", Kingdee.BOS.KDDbType.Int64, userId));
            var client = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            objs = client.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);

            return objs;
        }

        List<Int64> GetOrgIds(Int64 userId)
        {
            var orgIds = new List<Int64>();
            var sql = string.Format(@"
SELECT FORGID FROM t_sec_userorg
WHERE FUSERID=@userId
                ");
            //WHERE FUSERID=@userId
            var lst = new List<Kingdee.BOS.SqlParam>();
            lst.Add(new Kingdee.BOS.SqlParam("@userId", Kingdee.BOS.KDDbType.Int64, userId));
            var client = new SD.WRZS.K3.ServiceFacade.KDServiceClient.WRZSClientProxy();
            var objs = client.ExecuteDynamicObject(sql, System.Data.CommandType.Text, lst);
            foreach (var obj in objs)
            {
                var orgId = Convert.ToInt64(obj["FORGID"]);
                orgIds.Add(orgId);
            }
            return orgIds;
        }


    }
}